package com.canyinghao.cananimation.demo;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;

public class MainAbility extends Ability implements Component.ClickedListener {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        initView();
    }

    private void initView() {
        Button btn1 = (Button) findComponentById(ResourceTable.Id_btn_animation1);
        Button btn2 = (Button) findComponentById(ResourceTable.Id_btn_animation2);
        Button btn3 = (Button) findComponentById(ResourceTable.Id_btn_animation3);
        Button btn4 = (Button) findComponentById(ResourceTable.Id_btn_animation4);


        btn1.setClickedListener(this::onClick);
        btn2.setClickedListener(this::onClick);
        btn3.setClickedListener(this::onClick);
        btn4.setClickedListener(this::onClick);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_btn_animation1:
                startPage(FlipperAnimeAbility.class.getName());
                break;
            case ResourceTable.Id_btn_animation2:
                startPage(FlipperDemoAbility.class.getName());
                break;
            case ResourceTable.Id_btn_animation3:
                startPage(AnimationAbility.class.getName());
                break;
            case ResourceTable.Id_btn_animation4:
                startPage(AnimatorAbility.class.getName());
                break;
            default:
        }
    }

    /**
     * 页面跳转
     *
     * @param className
     */
    private void startPage(String className) {

        Intent secondIntent = new Intent();
        // 指定待启动FA的bundleName和abilityName
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName(getAbilityPackageContext().getBundleName())
                .withAbilityName(className)
                .build();
        secondIntent.setOperation(operation);
        startAbility(secondIntent);
    }
}
